// pages/index/searchPill/searchPill.js
import request from '../../../utils/request'

Page({
    data: {
        keyword: '',
        searchType: 'medicine', 
        medicines: [],
        searchHistory: [],
        filteredInstitutions: []
    },

    onLoad() {
        const history = wx.getStorageSync('searchHistory') || []
        this.setData({
            searchHistory: history
        })
    },

    onInput(e) {
        this.setData({
            keyword: e.detail.value
        })
    },

    clearInput() {
        this.setData({
            keyword: '',
            medicines: [],
            filteredInstitutions: []
        })
    },

    // 切换搜索类型
    switchType(e) {
        const type = e.currentTarget.dataset.type
        this.setData({
            searchType: type
        })
        if (this.data.keyword) {
            this.search()
        }
    },

    // 封装接口调用函数
    searchAPI(url, dataKey, successCallback) {
        let data = {};
        if (this.data.searchType === 'medicine') {
            data.name = this.data.keyword;
        } 
        request({
            url,
            method: 'GET',
            data,
            success: (res) => {
                console.log(`搜索 ${this.data.searchType} 接口返回数据:`, res.data.rows || []);
                successCallback(res.data.rows || [])
            },
            fail: (err) => {
                console.error(`搜索 ${this.data.searchType} 接口失败:`, err);
                wx.showToast({
                    title: '搜索失败，请检查网络连接',
                    icon: 'none'
                })
            }
        })
    },

    search() {
        const { keyword, searchType } = this.data
        if (!keyword.trim()) {
            wx.showToast({
                title: '请输入搜索内容',
                icon: 'none'
            })
            return
        }

        this.saveSearchHistory(keyword)

        // 根据搜索类型调用相应的接口
        if (searchType === 'medicine') {
            // 调用搜索接口
            this.searchAPI('sysmanagement/medicine/list', 'medicines', (data) => {
                this.setData({
                    medicines: data
                })
            })
        }
    },

    saveSearchHistory(keyword) {
        let history = wx.getStorageSync('searchHistory') || []
        if (!history.includes(keyword)) {
            history.unshift(keyword)
            if (history.length > 10) {
                history = history.slice(0, 10)
            }
            wx.setStorageSync('searchHistory', history)
            this.setData({
                searchHistory: history
            })
        }
    },

    onHistoryTap(e) {
        const keyword = e.currentTarget.dataset.keyword
        this.setData({ keyword }, () => {
            this.search()
        })
    },

    clearHistory() {
        wx.showModal({
            title: '提示',
            content: '确定要清空搜索历史吗？',
            success: (res) => {
                if (res.confirm) {
                    wx.removeStorageSync('searchHistory')
                    this.setData({
                        searchHistory: []
                    })
                }
            }
        })
    },

    // 跳转到药品详情页
    goTomedicineDetail(e) {
        const medicine = e.currentTarget.dataset.medicine
        wx.navigateTo({
            url: ``
        })
    },
})
    